/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 * Description: UDK byte order header file
 * Author: -
 * Create: 2021.4.20
 */

#ifndef UDK_BYTEORDER_H
#define UDK_BYTEORDER_H

#include <endian.h>

#ifndef USE_GCC_DEFAULT_ENDIAN

#ifdef UDK_ARCH_ARM64
#include "arch/arm/udk_byteorder.h"
#else
#include "arch/x86/udk_byteorder.h"
#endif

#if __BYTE_ORDER == __LITTLE_ENDIAN

#define udk_cpu_to_le_16(x) (x)
#define udk_cpu_to_le_32(x) (x)
#define udk_cpu_to_le_64(x) (x)

#define udk_cpu_to_be_16(x) udk_bswap16(x)
#define udk_cpu_to_be_32(x) udk_bswap32(x)
#define udk_cpu_to_be_64(x) udk_bswap64(x)

#define udk_le_to_cpu_16(x) (x)
#define udk_le_to_cpu_32(x) (x)
#define udk_le_to_cpu_64(x) (x)

#define udk_be_to_cpu_16(x) udk_bswap16(x)
#define udk_be_to_cpu_32(x) udk_bswap32(x)
#define udk_be_to_cpu_64(x) udk_bswap64(x)

#else /* __BYTE_ORDER == __BIG_ENDIAN */

#define udk_cpu_to_le_16(x) udk_bswap16(x)
#define udk_cpu_to_le_32(x) udk_bswap32(x)
#define udk_cpu_to_le_64(x) udk_bswap64(x)

#define udk_cpu_to_be_16(x) (x)
#define udk_cpu_to_be_32(x) (x)
#define udk_cpu_to_be_64(x) (x)

#define udk_le_to_cpu_16(x) udk_bswap16(x)
#define udk_le_to_cpu_32(x) udk_bswap32(x)
#define udk_le_to_cpu_64(x) udk_bswap64(x)

#define udk_be_to_cpu_16(x) (x)
#define udk_be_to_cpu_32(x) (x)
#define udk_be_to_cpu_64(x) (x)

#endif /* end __BYTE_ORDER == __LITTLE_ENDIAN  */

#else /* USE_GCC_DEFAULT_ENDIAN */

#define udk_cpu_to_le_16(x) htole16(x)
#define udk_cpu_to_le_32(x) htole32(x)
#define udk_cpu_to_le_64(x) htole64(x)

#define udk_cpu_to_be_16(x) htobe16(x)
#define udk_cpu_to_be_32(x) htobe32(x)
#define udk_cpu_to_be_64(x) htobe64(x)

#define udk_le_to_cpu_16(x) le16toh(x)
#define udk_le_to_cpu_32(x) le32toh(x)
#define udk_le_to_cpu_64(x) le64toh(x)

#define udk_be_to_cpu_16(x) be16toh(x)
#define udk_be_to_cpu_32(x) be32toh(x)
#define udk_be_to_cpu_64(x) be64toh(x)

#endif /* end USE_GCC_DEFAULT_ENDIAN */

#endif /* end UDK_BYTEORDER_H */